package com.xuecheng.content.controller;

import com.xuecheng.commons.model.dto.CourseAuditDto;
import com.xuecheng.commons.model.dto.CourseBaseDto;
import com.xuecheng.commons.model.dto.CourseDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.vo.CourseBaseVo;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.config.MinioTemplate;
import com.xuecheng.content.domain.CourseBase;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 课程基本信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
public class CourseBaseController {

    @Autowired
    private CourseBaseService courseBaseService;

    // 课程信息分页查询
    @PostMapping("/course/list")
    public ResponseResult findByPage(PageRequest request, @RequestBody CourseDto dto) {
        return courseBaseService.findByPage(request, dto);
    }

    @Autowired
    private MinioTemplate minioTemplate;

    // 课程封面上传
    @PostMapping("/course/upload")
    public ResponseResult upload(MultipartFile uploadFile) throws IOException {
        // 上传图片到minio
        String url = minioTemplate.upload(uploadFile.getInputStream(), uploadFile.getOriginalFilename(), uploadFile.getContentType());
        // 返回结果
        return ResponseResult.okResult(url);
    }

    // 课程信息新增
    @PostMapping("/course")
    public ResponseResult saveCourseBase(@RequestBody CourseBaseDto dto) {
        return courseBaseService.saveCourseBase(dto);
    }

    // 课程信息回显
    @GetMapping("/course/{courseBaseId}")
    public ResponseResult findCourseBaseById(@PathVariable("courseBaseId") Long courseBaseId) {
        // 调用service查询数据库
        CourseBase courseBase = courseBaseService.getById(courseBaseId);
        // courseBase(entity) -> courseBaseVo
        CourseBaseVo vo = BeanHelper.copyProperties(courseBase, CourseBaseVo.class);
        // 返回结果
        return ResponseResult.okResult(vo);
    }

    // 课程信息修改
    @PutMapping("/course")
    public ResponseResult updateCourseBase(@RequestBody CourseBaseDto dto) {
        return courseBaseService.updateCourseBase(dto);
    }

    // 课程信息删除
    @DeleteMapping("/course/{courseId}")
    public ResponseResult deleteCourseBaseById(@PathVariable("courseId") Long courseId) {
        return courseBaseService.deleteCourseBaseById(courseId);
    }


    // 提交审核
    @GetMapping("/course/commit/{courseBaseId}")
    public ResponseResult commit(@PathVariable("courseBaseId") Long courseBaseId){
        return courseBaseService.commit(courseBaseId);
    }

    // 平台审核
    @PostMapping("/course/approve")
    public ResponseResult approve(@RequestBody CourseAuditDto dto){
        return courseBaseService.approve(dto);
    }
}
